package com.bjsxt.ego.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.ego.common.util.ConstatFinalUtil;
import com.bjsxt.ego.common.util.PageInfoUtil;
import com.bjsxt.ego.users.dao.IAdminsDao;
import com.bjsxt.ego.users.dao.IRoleDao;
import com.bjsxt.ego.users.pojo.Admins;
import com.bjsxt.ego.users.pojo.Role;
import com.bjsxt.ego.users.service.IUsersService;
/**
 *  用户模块相关的service接口的实现类
 * @author 章鸿敏
 *
 */
@Service("usersService")
public class UsersServiceImpl implements IUsersService
{
	@Resource
	private IAdminsDao adminsDao;
	@Resource
	private IRoleDao roleDao;

	/**********************************管理员模块开始******************************/
	/**
	 *  保存一条管理员
	 * @param admins
	 * @return
	 */
	@Override
	public JSONObject saveOneAdminsService(Admins admins)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.adminsDao.save(admins);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	/**
	 * 更新一条管理员
	 */
	@Override
	public JSONObject updateOneAdminsService(Admins admins)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.adminsDao.update(admins);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	/**
	 * 删除一条管理员记录
	 */
	@Override
	public JSONObject deleteOneAdminsService(Admins admins)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.adminsDao.delete(admins);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public Admins findOneAdminsService(Map<String, Object> condMap)
	{
		return this.adminsDao.findOne(condMap);
	}

	@Override
	public List<Admins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 为关键字加上%，实现模糊匹配*/
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword")+""))
		{
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if (pageInfoUtil != null)
		{
			/* 分页*/
			/* 查询总的记录数*/
			condMap.put("pageMarking", "true");
			List<Admins> adminsList =this.adminsDao.findCondList(condMap);
			/* 取得总记录数*/
			if (adminsList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(adminsList.get(0).getId());
			}
			
			/*开始分页,查询数据*/
			condMap.put("pageMarking", "false");
			/*limit ?,?*/
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.adminsDao.findCondList(condMap);
		}
		/*不分页查询数据*/
		return this.adminsDao.findCondList(condMap);
	}
	
	/**********************************管理员模块结束******************************/
	
	/**********************************角色模块开始******************************/
	/**
	 *  保存一条角色
	 * @param Role
	 * @return
	 */
	@Override
	public JSONObject saveOneRoleService(Role Role)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.roleDao.save(Role);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", Role.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	/**
	 * 更新一条角色
	 */
	@Override
	public JSONObject updateOneRoleService(Role Role)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.roleDao.update(Role);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", Role.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	/**
	 * 删除一条角色记录
	 */
	@Override
	public JSONObject deleteOneRoleService(Role Role)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.roleDao.delete(Role);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", Role.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	@Override
	public Role findOneRoleService(Map<String, Object> condMap)
	{
		return this.roleDao.findOne(condMap);
	}

	@Override
	public List<Role> findCondListRoleService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 为关键字加上%，实现模糊匹配*/
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword")+""))
		{
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if (pageInfoUtil != null)
		{
			/* 分页*/
			/* 查询总的记录数*/
			condMap.put("pageMarking", "true");
			List<Role> RoleList =this.roleDao.findCondList(condMap);
			/* 取得总记录数*/
			if (RoleList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(RoleList.get(0).getId());
			}
			
			/*开始分页,查询数据*/
			condMap.put("pageMarking", "false");
			/*limit ?,?*/
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.roleDao.findCondList(condMap);
		}
		/*不分页查询数据*/
		return this.roleDao.findCondList(condMap);
	}
	
	/**********************************角色模块结束******************************/
	

}
